async function uploadImage() {
    const fileInput = document.getElementById('imageInput');
    const uploadArea = document.getElementById('uploadArea');
    const resultArea = document.getElementById('resultArea');
    const previewImage = document.getElementById('previewImage');
    const predResult = document.getElementById('predResult');
    const confidence = document.getElementById('confidence');
    const filename = document.getElementById('filename');

    if (fileInput.files.length === 0) {
        alert('请先选择图片');
        return;
    }

    // 显示预览
    const file = fileInput.files[0];
    previewImage.src = URL.createObjectURL(file);
    resultArea.style.display = 'block';

    // 显示加载状态
    predResult.textContent = '识别中...';
    confidence.textContent = '计算中...';
    filename.textContent = file.name;

    const formData = new FormData();
    formData.append('file', file);

    try {
        const response = await fetch('/upload/', {
            method: 'POST',
            body: formData
        });

        if (!response.ok) {
            throw new Error(`HTTP error! status: ${response.status}`);
        }

        const data = await response.json();

        // 更新结果
        predResult.textContent = data.prediction;
        confidence.textContent = data.confidence;
        filename.textContent = data.filename;
        previewImage.src = data.image_url;

    } catch (error) {
        console.error('识别失败:', error);
        predResult.textContent = '识别失败';
        confidence.textContent = error.message;
    }
}

// 拖拽上传功能
const uploadBox = document.querySelector('.upload-box');
uploadBox.addEventListener('dragover', (e) => {
    e.preventDefault();
    uploadBox.style.borderColor = '#4a6bdf';
    uploadBox.style.backgroundColor = 'rgba(74, 107, 223, 0.1)';
});

uploadBox.addEventListener('dragleave', (e) => {
    e.preventDefault();
    uploadBox.style.borderColor = '#ccc';
    uploadBox.style.backgroundColor = 'transparent';
});

uploadBox.addEventListener('drop', (e) => {
    e.preventDefault();
    uploadBox.style.borderColor = '#ccc';
    uploadBox.style.backgroundColor = 'transparent';

    const fileInput = document.getElementById('imageInput');
    fileInput.files = e.dataTransfer.files;
});
